package com.be.beadmin.material.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.material.dto.input.PickingDetailsDto;
import com.be.beadmin.material.dto.input.PtCodeAndPage;
import com.be.beadmin.material.dto.output.PickingDetailsEx;
import com.be.beadmin.material.entity.PickingDetails;
import com.be.beadmin.material.repository.PickingDetailsRepository;
import com.be.beadmin.material.service.PickingDetailsService;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;

/**
* @author zmy
* @date 2023-05-14
**/
@Api(tags = "物料管理-分料细节")
@RestController
public class PickingDetailsController {

    @Autowired
    private PickingDetailsService pickingDetailsService;
    @Autowired
    private PickingDetailsRepository pickingDetailsRepository;

    @ApiParam(value = "分页获取分料细节信息")
    @ApiOperation("分页获取分料细节信息1")
    @PostMapping(value = "/product/getPickingDetailsByPage")
    public ResultModel getPickingDetailsByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(pickingDetailsService.getPickingDetailsByPage(pageEntity)) ;
    }

    //TODO
    @ApiParam(value = "根据批次计划获取分料细节信息")
    @ApiOperation("根据批次计划获取分料细节信息1")
    @PostMapping(value = "/product/getPickingDetailsByBatchPlanId")
    public ResultModel getPickingDetailsByBatchPlanId(@ApiParam("id") @RequestParam String id)  {

        return ResultModel.success(pickingDetailsService.getPickingDetailsByBatchPlanId(id)) ;
    }

    @ApiParam(value = "分页获取分料细节信息")
    @PostMapping(value = "/product/getPickingDetailsByPageAndPTcode")
    public ResultModel getPickingDetailsByPageAndPTcode(@RequestBody PtCodeAndPage pageEntity)  {
        return ResultModel.success(pickingDetailsService.getPickingDetailsByPageAndPTcode(pageEntity)) ;
    }

    @PostMapping("/product/addPickingDetails")
    public ResultModel addPickingDetails(@RequestBody PickingDetailsDto pickingDetailsDto){
        pickingDetailsService.addPickingDetails(pickingDetailsDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delPickingDetails")
    public ResultModel delPickingDetails(@RequestBody PickingDetailsDto pickingDetailsDto){
        pickingDetailsService.delPickingDetails(pickingDetailsDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updatePickingDetails")
    public ResultModel updatePickingDetails(@RequestBody PickingDetailsDto pickingDetailsDto){
        pickingDetailsService.updatePickingDetails(pickingDetailsDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportPickingDetails")
    public void exportPickingDetails(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PickingDetails> pickingDetailsPage = pickingDetailsService.getPickingDetailsByPage(pageEntity);
        List<PickingDetails> pickingDetailsExes = pickingDetailsPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PickingDetails接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PickingDetailsEx.class).sheet("sheet1").doWrite(pickingDetailsExes);
    }
    //弃用
    @ApiOperation("分料接口，扫描物料编码和产品编号，")
    @PostMapping("/product/addMaterialQty")
    public ResultModel addMaterialQtyByMaterialCodeAndPickingTaskCode(@ApiParam("物料编码") @RequestParam String materialCode,
                                                                      @ApiParam("分料任务号") @RequestParam String pickingTaskCode)
                                                                      //@ApiParam(value = "分料数量" ,required = false) @RequestParam(required = false) int pickingNum)
                                                                      {
        pickingDetailsService.addMaterialQtyByMaterialCodeAndPickingTaskCode(materialCode,pickingTaskCode);
        return ResultModel.success("分料成功");
    }

    @ApiOperation("分料接口")
    @PostMapping("/product/allocation")
    public ResultModel allocation(@ApiParam("id") @RequestParam String id,
                                  @ApiParam(value = "分料数量" ) @RequestParam int pickingNum) throws ParseException {
        pickingDetailsService.allocation(id,pickingNum);
        return ResultModel.success("分料成功");
    }

    @ApiOperation("分料接口2")
    @PostMapping("/product/allocation2")
    public ResultModel allocation2(@ApiParam("pickingdetails_id") @RequestParam String pickingdetails_id,
                                   @ApiParam(value = "未分数量") @RequestParam int notissueqty,
                                   @ApiParam(value = "已分数量") @RequestParam int outboundQty) throws ParseException {
//        pickingDetailsService.allocation(id,pickingNum);
        pickingDetailsService.allocation2(pickingdetails_id, notissueqty, outboundQty);
        return ResultModel.success("分料成功");
    }


    @ApiOperation("分料接口3")
    @PostMapping("/product/allocation3")
    public ResultModel allocation3(@ApiParam("batchPlanId") @RequestParam String batchPlanId,
                                   @ApiParam(value = "未分数量") @RequestParam double notissueqty,
                                   @ApiParam(value = "已分数量") @RequestParam double outboundQty,
                                   @ApiParam(value = "物料编码") @RequestParam String materialCode) throws ParseException {
//        pickingDetailsService.allocation(id,pickingNum);
        pickingDetailsService.allocation3(batchPlanId, notissueqty, outboundQty, materialCode);
        return ResultModel.success("分料成功");
    }

    @ApiOperation("扫描序列号")
    @PostMapping("/product/scanSerNo")
    public ResultModel scanSerNo(@ApiParam("id") @RequestParam String id,
                                  @ApiParam(value = "序列号" ) @RequestParam String SerNo) {
        pickingDetailsService.scanSerNo(id,SerNo);
        return ResultModel.success("扫描序列号成功！");
    }

    @ApiOperation(value = "模糊查询")
    @PostMapping(value = "/product/getPickingDetailsByPageLike")
    public ResultModel getPickingDetailsByPageLike(@RequestBody PageEntity pageEntity,
                                                   @ApiParam("搜索内容") @RequestParam String like,
                                                   @ApiParam("分料任务号") @RequestParam String pickingTaskCode)  {
        return ResultModel.success(pickingDetailsService.getPickingDetailsByPageLike(pageEntity,like,pickingTaskCode)) ;
    }

    @ApiOperation(value = "模糊查询2")
    @PostMapping(value = "/product/getPickingDetailsByPageLike2")
    public ResultModel getPickingDetailsByPageLike2(@RequestBody PageEntity pageEntity,
                                                   @ApiParam("搜索内容") @RequestParam String like,
                                                   @ApiParam("分料任务号") @RequestParam String pickingTaskCode)  {
        return ResultModel.success(pickingDetailsService.getPickingDetailsByPageLike2(pageEntity,like,pickingTaskCode)) ;
    }

    @ApiOperation(value = "打印条码")
    @GetMapping(value = "/product/getBatchBarcodesByPickingTaskCode")
    public void getBatchBarcodesByPickingTaskCode(HttpServletResponse response ,
                                                   @ApiParam("物料编码") @RequestParam String materialCode,
                                                  @ApiParam("物料规格") @RequestParam String materialSpec) throws IOException, WriterException {
        String msg = "MaterialCode:"+materialCode+";MaterialSpec:"+materialSpec;


        Code128Bean bean = new Code128Bean();
        final int dpi = 100; // 设置图像dpi
        bean.setModuleWidth(0.4);

        bean.setBarHeight(35);
        bean.doQuietZone(true);
        bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);

        boolean antiAlias = false;
        int orientation = 0; // 设置条形码的方向

        // 生成条形码图片
        BitmapCanvasProvider canvas = new BitmapCanvasProvider(response.getOutputStream(), "image/png", dpi, BufferedImage.TYPE_BYTE_BINARY, antiAlias, orientation);
        bean.generateBarcode(canvas, msg);

        // 完成条形码的绘制
        canvas.finish();

        // 设置响应头，告诉浏览器以附件形式下载文件
        response.setHeader("Content-Disposition", "attachment; filename=\"barcode.png\"");

        // 关闭响应输出流
        response.getOutputStream().close();


    }


}